package myproject;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.MidiDevice.Info;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import myproject.sys.entity.User;
import myproject.sys.server.UserService;

@Controller 
public class HelloController  {

	@Autowired
	private UserService userService;
	
	 
	@RequestMapping(value = "/hello")                  //②请求URL到处理器功能处理方法的映射  
	    public ModelAndView helloWorld() {  
	        //1、收集参数  
	        //2、绑定参数到命令对象  
	        //3、调用业务对象  
	        //4、选择下一个页面  
	        ModelAndView mv = new ModelAndView();  
	        User user=(User) userService.selectUserByUserName("zhangsan").get(0);
	        //添加模型数据 可以是任意的POJO对象  
	        mv.addObject("message", user.getUsername());  
	        //设置逻辑视图名，视图解析器会根据该名字解析到具体的视图页面  
	        
	        
	        mv.setViewName("hello");  
	        return mv;                                         //○3 模型数据和逻辑视图名  
	    }  
	
	
	@ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public JSONObject login(HttpServletRequest request, HttpServletResponse response){
		JSONObject json=new JSONObject();
        String username = request.getParameter("username");
        User user = userService.selectUserByUserName(username).get(0);

        if(user != null && user.getPassword().equals(request.getParameter("userpassword"))){
           
            json.put("result", "success");
            return json;
        }

        return json;
    }
	
	@RequestMapping(value = "/regist",method=RequestMethod.POST,produces = "application/json;charset=UTF-8") 
	public @ResponseBody ModelAndView handle4Mobile(@RequestBody  Info info){
		System.out.println(info.getName()); 
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "sucess");
		return new ModelAndView(new MappingJackson2JsonView(),map);

	}
}
